package com.jcfk.eam.controller.main;

import cn.hutool.core.util.StrUtil;
import com.jcfk.common.business.log.annotation.RequestLogHeader;
import com.jcfk.common.security.utils.SecurityUtils;
import com.jcfk.common.web.domain.DataPage;
import com.jcfk.common.web.domain.ReqPageParam;
import com.jcfk.common.web.domain.RetPage;
import com.jcfk.common.web.domain.RetResult;
import com.jcfk.eam.domain.dto.base.EamRepairOrderDTO;
import com.jcfk.eam.domain.dto.base.EamRepairOrderStatusDTO;
import com.jcfk.eam.domain.dto.base.EamRepairOrderTransmitDTO;
import com.jcfk.eam.domain.query.base.EamRepairOrderQuery;
import com.jcfk.eam.service.chat.EnterpriseWechatService;
import com.jcfk.eam.service.main.EamRepairOrderService;
import com.jcfk.eam.service.main.EamRepairOrderTransmitService;
import com.jcfk.eam.service.main.EamWorkOrderRecordService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * 维修单
 *
 * @author awei
 * @email 522891237@gqq.com
 * @date 2024-08-26
 */
@Api(tags = {"故障维修"})
@RestController
@RequestMapping("eamrepairorder")
public class EamRepairOrderController {
    @Autowired
    private EamRepairOrderService eamRepairOrderService;

    @Autowired
    private EamRepairOrderTransmitService eamRepairOrderTransmitService;

    @Autowired
    private EamWorkOrderRecordService eamWorkOrderRecordService;

    @Autowired
    private EnterpriseWechatService enterpriseWechatService;

    /**
     * 分页列表
     */
    @ApiImplicitParams({@ApiImplicitParam(paramType = "body", dataType = "ReqPageParam<EamRepairOrderQuery>", name = "requestParam", value = "", required = true)})
    @ApiOperation("分页查询维修单")
    @PostMapping("/queryByPage")
    public RetPage<EamRepairOrderDTO> queryByPage(@RequestBody ReqPageParam<EamRepairOrderQuery> requestParam) {
        DataPage<EamRepairOrderDTO> page = new DataPage<EamRepairOrderDTO>((long) requestParam.getCurrent(), (long) requestParam.getSize(), requestParam.getSort().getOrderItem(), EamRepairOrderDTO.class);
        page = eamRepairOrderService.queryByPage(page, requestParam.getData());

        return new RetPage<>(page);
    }

    /**
     * 分页查询我的报修单
     */
    @ApiImplicitParams({@ApiImplicitParam(paramType = "body", dataType = "ReqPageParam<EamRepairOrderQuery>", name = "requestParam", value = "", required = true)})
    @ApiOperation("分页查询我的报修单")
    @PostMapping("/queryMyReportOrder")
    public RetPage<EamRepairOrderDTO> queryMyReportOrder(@RequestBody ReqPageParam<EamRepairOrderQuery> requestParam) {
        DataPage<EamRepairOrderDTO> page = new DataPage<EamRepairOrderDTO>((long) requestParam.getCurrent(), (long) requestParam.getSize(), requestParam.getSort().getOrderItem(), EamRepairOrderDTO.class);
        requestParam.getData().setRepairUser(SecurityUtils.getUserId());

        page = eamRepairOrderService.queryByPage(page, requestParam.getData());

        return new RetPage<>(page);
    }

    /**
     * 分页查询备件使用统计
     */
    @ApiImplicitParams({@ApiImplicitParam(paramType = "body", dataType = "ReqPageParam<EamRepairOrderQuery>", name = "requestParam", value = "", required = true)})
    @ApiOperation("分页查询备件使用统计")
    @PostMapping("/queryUsageStatistics")
    public RetPage<EamRepairOrderDTO> queryUsageStatistics(@RequestBody ReqPageParam<EamRepairOrderQuery> requestParam) {
        DataPage<EamRepairOrderDTO> page = new DataPage<EamRepairOrderDTO>((long) requestParam.getCurrent(), (long) requestParam.getSize(), requestParam.getSort().getOrderItem(), EamRepairOrderDTO.class);
        page = eamRepairOrderService.queryUsageStatistics(page, requestParam.getData());
        return new RetPage<>(page);
    }

    /**
     * 分页查询我的维修单
     */
    @ApiImplicitParams({@ApiImplicitParam(paramType = "body", dataType = "ReqPageParam<EamRepairOrderQuery>", name = "requestParam", value = "", required = true)})
    @ApiOperation("分页查询我的维修单")
    @PostMapping("/queryMyRepairOrder")
    public RetPage<EamRepairOrderDTO> queryMyRepairOrder(@RequestBody ReqPageParam<EamRepairOrderQuery> requestParam) {
        DataPage<EamRepairOrderDTO> page = new DataPage<EamRepairOrderDTO>((long) requestParam.getCurrent(), (long) requestParam.getSize(), requestParam.getSort().getOrderItem(), EamRepairOrderDTO.class);
        requestParam.getData().setRepairTeamSysUser(SecurityUtils.getUserId());

        page = eamRepairOrderService.queryMyRepairOrder(page, requestParam.getData());

        return new RetPage<>(page);
    }

    /**
     * 维修单
     * 根据id 查询详情
     */
    @ApiOperation("查询维修单")
    @GetMapping("/get/{id}")
    public RetResult<EamRepairOrderDTO> get(@PathVariable("id") String id) {
        EamRepairOrderDTO item = eamRepairOrderService.get(id);
        return RetResult.ok(item);
    }

    /**
     * 添加/更新管理维修单
     */
    @ApiOperation("保存维修单")
    @PostMapping("/save")
    @RequestLogHeader(modular = "维修单管理", businessType = "保存")
    public RetResult<?> save(@RequestBody @Valid EamRepairOrderDTO eamRepairOrder) {
        // 数据校验
        RetResult ret = eamRepairOrderService.isValid(eamRepairOrder);
        if (!ret.getCode().equals(RetResult.SUCCESS)) {
            return ret;
        }

        if (StrUtil.isEmpty(eamRepairOrder.getId())) {
            // 新增
            eamRepairOrderService.insert(eamRepairOrder);
            enterpriseWechatService.sendWeChat(eamRepairOrder.getAssetName(), eamRepairOrder);
        } else {
            // 更新
            eamRepairOrderService.update(eamRepairOrder);
        }
        return RetResult.ok();

    }

    /**
     * 批量删除维修单
     */
    @ApiOperation("批量删除维修单")
    @PostMapping("/delete")
    @RequestLogHeader(modular = "维修单管理", businessType = "批量删除")
    public RetResult<?> delete(@RequestBody EamRepairOrderDTO eamRepairOrder) {
        return RetResult.ok(eamRepairOrderService.logicDeleteList(eamRepairOrder));
    }


    /**
     * 删除维修单
     */
    @ApiOperation("删除维修单")
    @PostMapping("/deleteItem/{id}")
    @RequestLogHeader(modular = "维修单管理", businessType = "删除")
    public RetResult<?> deleteItem(@PathVariable("id") String id) {
        eamRepairOrderService.deleteItem(id);
        return RetResult.ok();
    }


    /**
     * 修改维修单状态
     */
    @ApiOperation("修改维修单状态")
    @PostMapping("/updateStatus")
    @RequestLogHeader(modular = "维修单管理", businessType = "修改维修单状态")
    public RetResult<?> updateStatus(@RequestBody EamRepairOrderStatusDTO eamRepairOrderDTO) {
        eamRepairOrderService.updateStatus(eamRepairOrderDTO);
        return RetResult.ok();
    }


    /**
     * 委外开始
     */
    @ApiOperation("委外开始")
    @PostMapping("/start/outSourcing")
    @RequestLogHeader(modular = "维修单管理", businessType = "删除")
    public RetResult<?> outSourcingStart(@RequestBody EamRepairOrderStatusDTO eamRepairOrderDTO) {
        eamRepairOrderService.outSourcingStart(eamRepairOrderDTO);
        return RetResult.ok();
    }


    /**
     * 委外完成
     */
    @ApiOperation("委外完成")
    @PostMapping("/finish/outSourcing")
    @RequestLogHeader(modular = "维修单管理", businessType = "删除")
    public RetResult<?> outSourcingFinish(@RequestBody EamRepairOrderStatusDTO eamRepairOrderDTO) {
        eamRepairOrderService.outSourcingFinish(eamRepairOrderDTO);
        return RetResult.ok();
    }

    /**
     * 转交维修单
     */
    @ApiOperation("转交")
    @PostMapping("/transmit")
    @RequestLogHeader(modular = "维修单管理", businessType = "删除")
    public RetResult<?> transmit(@RequestBody EamRepairOrderTransmitDTO eamRepairOrderTransmitDTO) {
        eamRepairOrderService.transmit(eamRepairOrderTransmitDTO);

        return RetResult.ok();
    }


    /**
     * 指派维修单
     */
    @ApiOperation("指派")
    @PostMapping("/assign")
    @RequestLogHeader(modular = "维修单管理", businessType = "删除")
    public RetResult<?> assign(@RequestBody EamRepairOrderStatusDTO eamRepairOrderDTO) {
        eamRepairOrderService.assign(eamRepairOrderDTO);
        return RetResult.ok();
    }

}
